package com.qf.Servlet;

import com.qf.Service.MedicineInfoService;
import com.qf.Service.impl.MedicineInfoServiceImpl;
import com.qf.Utils.JsonUtils;
import com.qf.Utils.ResultVo;
import com.qf.entity.MedicineInfo;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(name = "medicineInfoServlet",urlPatterns = "/medicineInfoServlet")
public class MedicineInfoServlet extends BaseServlet {

    private MedicineInfoService medicineInfoService = new MedicineInfoServiceImpl();

    public void addmedicineInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
   //获取前端传递的参数
//        private String medicineName;
//        private long medicinePrice;
//        private String medicineValue;
        String  id = req.getParameter("id");
        String medicineName = req.getParameter("medicineName");
        String medicinePrice = req.getParameter("medicinePrice");
        String medicineValue = req.getParameter("medicineValue");
        //实例化药物
        MedicineInfo me =new MedicineInfo();
        me.setId(Integer.parseInt(id));
        me.setMedicine_Name(medicineName);
        me.setMedicine_Price(Integer.parseInt(medicinePrice));
        me.setMedicine_Value(medicineValue);
        int num = medicineInfoService.addMedicineInfo(me);
        PrintWriter pw = resp.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }

    public void updateMedicineInfo(HttpServletRequest req,HttpServletResponse resp) throws IOException, SQLException {
        String id = req.getParameter("id");
        String medicine_name = req.getParameter("medicine_name");
        String medicine_price = req.getParameter("medicine_price");
        String medicine_value = req.getParameter("medicine_value");
        MedicineInfo me =new MedicineInfo();
        me.setId(Integer.parseInt(id));
        me.setMedicine_Name(medicine_name);
        me.setMedicine_Price(Integer.parseInt(medicine_price));
        me.setMedicine_Value(medicine_value);
        int num = medicineInfoService.updateMedicineInfo(me);
        PrintWriter pw = resp.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }


        public void deleteMedicineInfo(HttpServletRequest request,HttpServletResponse response) throws IOException, SQLException {

        String id =request.getParameter("id");
        PrintWriter pw = response.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        int num = medicineInfoService.deleteMedicineInfo(Integer.parseInt(id));
        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }

    public void  selectMedicine_name(HttpServletRequest request,HttpServletResponse response) throws IOException {
        PrintWriter pw = response.getWriter();
        String medicineName = null;
        String medicine_name = request.getParameter("medicine_name");
        MedicineInfo medicineInfo = medicineInfoService.selectByMedicine_name(medicineName);
        ResultVo<MedicineInfo> categoryResultVo= new ResultVo<>();
        if (medicine_name !=null && !"".equals(medicine_name)){
           medicineName =medicine_name;
            categoryResultVo.setCode(500);
            categoryResultVo.setMessage("没有查询到药品");
            categoryResultVo.setData(null);
        } else {
            categoryResultVo.setCode(200);
            categoryResultVo.setMessage("成功");
            categoryResultVo.setData(medicineInfo);
        }
        JsonUtils.showJson(pw,categoryResultVo);
    }
    }


